package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"micro-bbs/helper"
	"micro-bbs/initialize"
	"micro-bbs/libs"
	"net/http"
	"strconv"
	"time"
)

type ThirdController struct {
}

func NewThirdController() *ThirdController {
	return nil
}


//发送验证码邮箱
func (this *ThirdController) SendCode(c *gin.Context){
	email := c.DefaultQuery("email", "")
	fmt.Println("email:",email)
	if email == "" {
		c.JSON(http.StatusOK, libs.ReturnJson(4000,"", gin.H{
		}))
		return
	}
	//验证邮箱是否缓存
	//emailCode,err := initialize.RedisClient.Get("reg_email_"+email).Result()
	//if err == nil && len(emailCode) > 0{
	//	//panic(err)
	//	c.JSON(http.StatusOK, libs.ReturnJson(200,"", gin.H{
	//		"email": email,
	//	}))
	//	return
	//}
	//获取随机验证码
	strcode := libs.GenValidateCode(6)
	code, _ := strconv.ParseInt(strcode, 10, 64)
	helper.SendEmail(email, code)
	//加入缓存
	initialize.RedisClient.Set("reg_email_"+email, code,300*time.Second)
	c.JSON(http.StatusOK, libs.ReturnJson(200,"", gin.H{
		"email": email,
	}))
}


